/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('CaseAuditDispathReviewResultCtrl', ['$scope','CommunityService','CaseCommonService','WarningAddService','$http','$modal',
                    'CaseAuditDispathReviewDetailResultService','CaseAuditDispathReviewResultService','$state','BaseURL','Modal','FwaTabsetService','$stateParams','WarningScreenService',
	                                function($scope,CommunityService,CaseCommonService,WarningAddService,$http,$modal,
                                             CaseAuditDispathReviewDetailResultService,CaseAuditDispathReviewResultService,$state,baseUrl,Modal,FwaTabsetService,$stateParams,WarningScreenService){
		 $scope.requestParam = {
	                page:"1",
	                pageSize:"20"
	            };
        $scope.communityService = CommunityService;
        CommunityService.initCodeListDatas();
        $scope.UrlParams = $stateParams;
		$scope.CaseAuditDispathReviewResultService = CaseAuditDispathReviewResultService;

		
		$scope.problemtab=[ {label:'事件处理',id:0,url:'views/hcpms/audit/caseAduitDispathDealProblem.html'},
            {label:'调查结果',id:1,url:'views/hcpms/audit/caseAduitDispathReviewProblem.html'}];

        $scope.selectedTable = $scope.problemtab[0];
        $scope.tabIndex = 0;
        $scope.tabClicks = function(tab) {
            $scope.selectedTabs = tab;
            $scope.tabIndex=tab.id;

            if($scope.tabIndex==0){
                initdealPorblemData();
            }
            if($scope.tabIndex==1){
                initProblem();
            }
        }
		
		//初始化请求参数
        var params = function (){
            var problemNo = "";
            if($stateParams.problemNo && !angular.isUndefined($stateParams.problemNo) ){
                problemNo = $stateParams.problemNo;
            }
            var params = {
                problemNo:problemNo,
                page:$scope.requestParam.page,
                pageSize:$scope.requestParam.pageSize
            }
            return params;
        }
        /**
         * 初始化医疗机构数据
         */
        function initdealPorblemData(){
            var parameter=params();
            CaseAuditDispathReviewResultService.loadDealProblemData(parameter).success(function(response){
                 $scope.treatmentOpinion=response.result.treatmentOpinion;
                 $scope.auditOpinion=response.result.auditOpinion;
            });
        }
        /**
         *获取暂存信息
         */
        $scope.surveyDatail={};
        var initOldOpinionDesc='';
        function  initProblem(){
            var parameter=params();
            var parameters={businessId:$stateParams.problemNo,
                moduleType:'sjModule',
                childModuleType:'review',
                businessType:'4'};
            CaseAuditDispathReviewDetailResultService.loadDealProblem(parameter).success(function(response){
                if(response.result){
                    if (response.result.treatmentOpinion) {
                        $scope.problem = response.result.treatmentOpinion;
                        initOldOpinionDesc = response.result.treatmentOpinion.opinionDesc;
                    }
                    if (response.result.auditOpinion) {
                        $scope.surveyDatail = response.result.auditOpinion;
                        $scope.surveyDatail.originalOpinionDesc = $scope.surveyDatail.opinionDesc;
                    }
                }
                CaseAuditDispathReviewDetailResultService.loadStorageData(parameters).success(function(response)
                {
                    if(response.result)
                    {
                        if(response.result.newOpinionDesc)
                        {
                            $scope.problem.opinionDesc = response.result.newOpinionDesc;
                            $scope.surveyDatail.oldOpinionDesc=response.result.newOpinionDesc;
                        }
                        if(response.result.opinionDesc)
                        {
                            $scope.surveyDatail.opinionDesc = response.result.opinionDesc;
                            $scope.surveyDatail.originalOpinionDesc = response.result.opinionDesc;
                        }
                        if(response.result.opinionState)
                        {
                            $scope.surveyDatail.opinionState  =response.result.opinionState;
                        }
                    }
                });
            });
        }
        /**
         * 初始化医疗机构数据和医护人员信息和参保人信息
         */
        $scope.medicalCode='';
        function initMedicalData(){
            var parameter=params();
            CaseCommonService.loadMedicalInfoList(parameter).success(function(response){
                $scope.medicalData2=response.medicalBean.medicalList;
                if($scope.medicalData2.length>0)
                {
                    if($scope.medicalData2[0].medicalCode)
                    {
                        $scope.medicalCode=$scope.medicalData2[0].medicalCode;
                    }
                }
                $scope.staffData2=response.staffBean.staffList;
                $scope.insuranceData2=response.insuranceBean.insuranceList;
                
                CaseCommonService.initStaffAndInsuranceTempList($scope);
				//更新当前页面数据
     		   WarningScreenService.findPointCutTempScose(
     				   WarningAddService.warningMarks,$stateParams.problemNo,'02',
     				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);

            });
        }

        /**
         * 初始化事件数据对象
         */
        $scope.object={};
        function initProblemData(){
            var parameter=params();
            CaseCommonService.loadProblemData(parameter).success(function(response){
                var obj=response.problemObj;
                $scope.object=obj;
            });
        }
        
        /**
         * 查看事件对象的案件来源对话框信息
         */
        $scope.$on('fwaAuditLeft.showModal',function(){
     	   $scope.problemObj=$scope.object;
     	   CaseCommonService.showProblemFromSourceDialog($scope);
        });
        /**
         * 医疗机构扣分事件
         */
        $scope.$on('fwaInvolve.medicalDeduction',function(e,v){
            $scope.marking (1,v, v.medicalCode, v.orgType);
        });
        /**
         * 医务人员扣分事件
         */
        $scope.$on('fwaInvolve.staffDeduction',function(e,v){
            $scope.marking(2,v,$scope.medicalCode,'');
        });

        /**
         * 调新增医务人员的回调函数
         *  parameter={
        * taskCode:'',
        * taskType:'',1事件,2案件
        * targetType:'',对象类型(1-医疗机构2-医生3-参保人)
        * targetCode:''
        * }
         */
        $scope.callStaffFun=function(array){
        	 CaseCommonService.addTempStaffList(array,$scope);
        }

        /**
         * 调新增参保人员的回调函数
         */
        $scope.callInsuranceFun=function(array){
        	 CaseCommonService.addTempInsuranceListInfo(array,$scope);
        }
        /**
         * 新增医护人员事件
         */
        $scope.$on('fwaInvolve.staffAdd',function(){
            CaseCommonService.addStaffFun($scope);
        });
        /**
         * 新增参保人员事件
         */
        $scope.$on('fwaInvolve.insuranceAdd',function() {
            CaseCommonService.addInsuranceFun($scope);
        });
        /**
         * 医务人员删除
         */
        $scope.$on('fwaInvolve.staffDel',function(e,data){
        	 Modal.confirm('确认删除医务人员'+data.name+'吗？',function(){
					WarningScreenService.deleteMark(WarningAddService.warningMarks,2,data);//mark:1.机构，2.医务人员，3.参保人
					 CaseCommonService.deleteTempStaffList(data,$scope);
				});
        });

        /**
         * 参保人员删除
         */
        $scope.$on('fwaInvolve.insuranceDel',function(e,data){
        	  Modal.confirm('确认删除参保人员'+data.name+'吗？',function(){
					WarningScreenService.deleteMark(WarningAddService.warningMarks,3,data);//mark:1.机构，2.医务人员，3.参保人
					 CaseCommonService.deleteTempInsuranceInfoList(data,$scope);
				});
        });

        /**
         * 点击医疗机构量事件
         * 
         */
        $scope.$on('fwaInvolve.medicalCountClick',function(){
     	   CaseCommonService.viewMedicalDetail($scope);
        });

        /**
         * 点击医务人员总数量事件
         */
        $scope.$on('fwaInvolve.staffCountClick',function(){
        	CaseCommonService.viewStaffList($scope);
        });
        
        /**
         * 点击参保人总数量事件
         */
        $scope.$on('fwaInvolve.insuranceCountClick',function(){
        	CaseCommonService.viewInsurList($scope);
        });
        
        
        /**
         * 返回
         */
        $scope.goBack = function () {
		   	WarningAddService.warningMarks = WarningAddService.warningMarkTemps;//初始化扣分参数
            if($stateParams.urlFlag){
                var urlFlag =  $stateParams.urlFlag;
                FwaTabsetService.closeTab('problemResearch.auditDispatchReviewResult','workingArea.transactorle',{urlFlag:urlFlag});
            }else
            {
                $state.go("problemResearch.problemResearchList");
            }
        };
        $scope.loading = false;
        $scope.submitForOpinion=function(flag)
        {
            if(check())
            {
                $scope.loading = true;
                $scope.surveyDatail.problemNo=$scope.UrlParams.problemNo;
                $scope.surveyDatail.newOpinionDesc=$scope.problem.opinionDesc;
                $scope.surveyDatail.onlySave=flag;
                $scope.surveyDatail.warnMarkTempDetails=WarningAddService.warningMarks;
                $scope.surveyDatail.tempFlag=1;//暂存标识：1，暂存；0，保存，2，退回岗位不同时
                if($scope.problem.opinionDesc==$scope.surveyDatail.oldOpinionDesc || !$scope.surveyDatail.oldOpinionDesc)
                {
                    $scope.surveyDatail.oldOpinionDesc=initOldOpinionDesc;
                }
                var parameter=$scope.surveyDatail;
                CaseAuditDispathReviewResultService.addSurResult(parameter).success(function(data){
                    if(data.state=='true')
                    {
                        if(flag=='0')
                        {
                            $scope.goBack();
                        }
                        initProblem();
                        Modal.alert(data.msg);
                        WarningAddService.warningMarks = [];
                      //更新当前页面数据
              		   WarningScreenService.findPointCutTempScose(
              				   WarningAddService.warningMarks,$stateParams.problemNo,'02',
              				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
                    }else
                    {
                        Modal.alert("添加失败！");
                    }
                    $scope.loading = false;
                });
                if(flag=='0')
                {
                    $scope.saveReallyStaffAndInsuarnceInfo();
                }else
                {
                    $scope.saveStaffAndInsuarnceInfo();
                }
            }
        }
        //扣分 mark：1.机构扣分，2.医务人员扣分，3参保人员扣分,data:数据集合
        $scope.marking = function(mark,data,medicalCode,orgType){
            var medicalCodeTemp=null;
            if(medicalCode&&!angular.isUndefined(medicalCode)){
                medicalCodeTemp = medicalCode;
            }
            if(mark != 1 && medicalCodeTemp == null){
                Modal.alert("请先增加医疗机构信息！");
                return null;
            }else{
                medicalCode = medicalCodeTemp;
            }
            markData(mark,data,medicalCode,orgType);//扣分数据参数加载
            var modalInstance = $modal.open({
                templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningDeductionDialog_view.html',
                backdrop:'static',
                size:'sm',
                scope: $scope,
//					  scope:scope,
                controller: 'WarningDeductionDialogCtrl'
            });
            //接收返回数据
            modalInstance.result.then(function(item) {
                if (!WarningAddService.warningMarks) {
                    WarningAddService.warningMarks = [];
                }
                WarningAddService.warningMarks = item;
                
              //更新当前页面数据
    		   	WarningScreenService.findPointCutTempScose(
    		   		item,$stateParams.problemNo,'02',
    		   	$scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
    		   	
            });
        }
        //扣分数据参数加载
        var markData = function(mark,data,medicalCode,orgType){
            WarningAddService.mark = mark;
            WarningAddService.warningMarkData = {
                orgType:orgType,//0.机构，1.药店
                medicalCode:medicalCode,//medicalCode:所属机构编号
                code:data.selfCode,//code:编号
                name:data.name,//name:名称
                caseType:'02',//案件类型caseType：01.预警,02.稽核
                caseNo: $stateParams.problemNo,//事件编号
                happenDate: $scope.object.hearDate,//受理日期
                punishDateStart: $scope.object.problemStart,// 事件开始日期
                punishDateEnd: $scope.object.problemEnd,// 事件结束日期
                warningMarks:WarningAddService.warningMarks
            };
        }
        function check()
        {
            if(!$scope.surveyDatail.opinionDesc)
            {
                Modal.alert('意见说明为必填项！');
                return false;
            }else  if(!$scope.surveyDatail.opinionState)
            {
                Modal.alert('复核意见为必填项！');
                return false;
            }else  if(!$scope.problem.opinionDesc)
            {
                Modal.alert('调查详情为必填项！');
                return false;
            }
            return true;
        }
        initProblemData();
        initMedicalData();
        $scope.tabClicks($scope.problemtab[1]);
        
        
        /**
         * 对医务人员和参保人员的暂存信息保存
         */
        $scope.saveStaffAndInsuarnceInfo=function(){
            $scope.saveType="temp";
            saveStaffListInfo();
            saveInsuranceInfo();
        }


        /**
         * 对医务人员和参保人员的真实数据信息保存
         */
        $scope.saveReallyStaffAndInsuarnceInfo=function(){
            $scope.saveType="save";
            saveStaffListInfo();
            saveInsuranceInfo();
        }

           /**
		    * 暂存的时候对医务人员进行的保存
		    */
	       function saveStaffListInfo(){
		   CaseCommonService.saveTempStaffListInfo($scope,'1','2',$stateParams.problemNo);
		   
	       }
	       
	       /**
		    * 暂存的时候对参保人员进行的保存
		    */
	       function saveInsuranceInfo(){
	    	   CaseCommonService.saveTempInsuranceListInfo($scope,'1','3',$stateParams.problemNo);
	       }

        }]);
})();
